<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class ManageraccessController extends BaseController {
    public function index(){
        //角色权限信息展示信息（显示所有）
    	$sql="select * from (select * from y_admin_role left JOIN y_admin_access on id=role_id) a LEFT JOIN y_admin_node b on a.node_id=b.id order by role_id asc";
    	$M = M();
    	$admin_access=$M->query($sql);
    	$role_name=array();
    	$role_access = [];
    	foreach($admin_access as $k=>$v){
    		if(in_array($v['role_name'],$role_name)){
    			$key = array_search($v['role_name'],$role_name); 			
    			$role_access[$key]['name'] = $role_access[$key]['name'].','.$v['name'];
    			$role_access[$key]['role'] = $v['role_name'];
    		}else{
    			$role_name[] = $v['role_name'];
    			$key = array_search($v['role_name'],$role_name);
    			$role_access[$key]['name'] = $v['name'];
    			$role_access[$key]['role'] = $v['role_name'];
    		}
    	}  
    	$this->assign("role_access",$role_access);
        $this->display("index");
    }
    public function edit_access(){
        //提取角色权限id方法
        $data=I("get.");
        $role_name=$data["role_name"];
        $sql="select * from (select * from y_admin_role INNER JOIN y_admin_access on id=role_id) a,y_admin_node b where a.node_id=b.id and role_name='$role_name'";
        $M = M();
        $admin_access=$M->query($sql);
        $role_name=array();
        $role_access = [];
        foreach($admin_access as $k=>$v){
            if(in_array($v['role_name'],$role_name)){
                $key = array_search($v['role_name'],$role_name);            
                $role_access[$key]['id'] = $role_access[$key]['id'].','.$v['id'];
                $role_access[$key]['role'] = $v['role_name'];
            }else{
                $role_name[] = $v['role_name'];
                $key = array_search($v['role_name'],$role_name);
                $role_access[$key]['id'] = $v['id'];
                $role_access[$key]['role'] = $v['role_name'];
            }
        }
        $this->assign("role_access",$role_access);
        $id=$role_access["0"]["id"];
        $id=explode(",",$id);
        $this->assign("id",$id);
        /*
         * 遍历所有的节点
         */
        $node = M('admin_node')->where("parent_id=0")->select();
        foreach($node as $k=>$v){
            $n_id[]=$v["id"];
        }
        $n_id=implode(",",$n_id);
        $child = M('admin_node')->where("parent_id in ($n_id)")->select();
        $data=array();
        foreach($node as $k=>$v){
            $data[$k] = $v;
            foreach($child as $key=>$vo){
                if($vo['parent_id'] == $v['id']){
                     $data[$k]['child'][] = $vo;
                    // if(in_array($vo['id'],$id)){
                    //     $data[$k]['child'][$key]['check']=1;
                    // }else{
                    //     $data[$k]['child'][$key]['check']=0;
                    // }
                }
            }
        }
        $this->assign("data",$data);
        $this->display();
    }
    /*
     * 提交权限信息
     */
    public function edit_access_do(){
        $data=I("post.");
        $access=$data["access"];
        $role_name=$data["role_name"];
        // dump($data);
        // dump($access);
        // dump($role_name);
        $M = M("admin_role");
        $id=$M->where("role_name='$role_name'")->select();
        $role_id=$id[0]["id"];
        // dump($role_id);
        // die;
        $M = M("admin_access");
        $M->where("role_id=$role_id ")->delete();
        $id=array();
        foreach($access as $k=>$v){
            $M = M();
            $sql="select parent_id from y_admin_node where id = $v";
            $parent_id = $M->query($sql);
            $parent_id =$parent_id[0]["parent_id"];
            $sql="insert into y_admin_access (role_id,node_id,parent_id) values ($role_id,$v,$parent_id)";
            $M->execute($sql);
            $id[]=$parent_id;
        }
        $id=array_unique($id);
        foreach($id as $k=>$v){
            $sql1="insert into y_admin_access (role_id,node_id,parent_id) values ($role_id,$v,0)";
            $M->execute($sql1); 
        }
       
        redirect(U("Manageraccess/index"),0,"页面跳转中···");
    }
    public function do_access(){
        //提取角色权限id方法
        $data=I("get.");
        $role_name=$data["role_name"];
        $this->assign("role_name",$role_name);
        $sql="select * from (select * from y_admin_role INNER JOIN y_admin_access on id=role_id) a,y_admin_node b where a.node_id=b.id and role_name='$role_name'";
        $M = M();
        $admin_access=$M->query($sql);
        $role_name=array();
        $role_access = [];
        foreach($admin_access as $k=>$v){
            if(in_array($v['role_name'],$role_name)){
                $key = array_search($v['role_name'],$role_name);            
                $role_access[$key]['id'] = $role_access[$key]['id'].','.$v['id'];
                $role_access[$key]['role'] = $v['role_name'];
            }else{
                $role_name[] = $v['role_name'];
                $key = array_search($v['role_name'],$role_name);
                $role_access[$key]['id'] = $v['id'];
                $role_access[$key]['role'] = $v['role_name'];
            }
        }
        $this->assign("role_access",$role_access);
        $id=$role_access["0"]["id"];
        $id=explode(",",$id);
        $this->assign("id",$id);
        /*
         * 遍历所有的节点
         */
        $node = M('admin_node')->where("parent_id=0")->select();
        foreach($node as $k=>$v){
            $n_id[]=$v["id"];
        }
        $n_id=implode(",",$n_id);
        $child = M('admin_node')->where("parent_id in ($n_id)")->select();
        $data=array();
        foreach($node as $k=>$v){
            $data[$k] = $v;
            foreach($child as $key=>$vo){
                if($vo['parent_id'] == $v['id']){
                     $data[$k]['child'][] = $vo;
                }
            }
        }
        $this->assign("data",$data);
        $this->display();
    }
    /*
     * 提交权限信息
     */
    public function do_access_do(){
        $data=I("post.");
        $access=$data["access"];
        $role_name=$data["role_name"];
        dump($data);
        // dump($access);
        // dump($role_name);
        $M = M("admin_role");
        $id=$M->where("role_name='$role_name'")->select();
        $role_id=$id[0]["id"];
        // dump($role_id);
        // die;
        // $M = M("admin_access");
        // $M->where("role_id=$role_id and parent_id!=0")->delete();
        foreach($access as $k=>$v){
            $M = M();
            $sql="select parent_id from y_admin_node where id = $v";
            $parent_id = $M->query($sql);
            $parent_id =$parent_id[0]["parent_id"];
            $sql="insert into y_admin_access (role_id,node_id,parent_id) values ($role_id,$v,$parent_id)";
            $M->execute($sql);

        }
        redirect(U("Manageraccess/index"),0,"页面跳转中···");
    }
}